<?php

// 黄页插件地址url
function dr_hy_url($uid) {

    $config = \Phpcmf\Service::M('app')->get_config('hy');
    if ($config['home']['urlrule']) {
        $data = dr_member_info($uid);
        if (!$data) {
            return 'uid(#'.$uid.')不存在';
        }
        return '/'.str_replace(['{username}', '{id}', '{uid}'], [$data['username'], $data['uid'], $data['uid']], ltrim($config['home']['urlrule'], '/'));
    }

    return '/index.php?s=hy&c=show&uid='.$uid;
}

function dr_hx_search_url($params = [], $name = '', $value = '') {
    if ($name) {
        if (is_array($name)) {
            foreach ($name as $i => $_name) {
                if (isset($value[$i]) && strlen($value[$i])) {
                    $params[$_name] = $value[$i];
                } else {
                    unset($params[$_name]);
                }
            }
        } else {
            if (strlen($value)) {
                $params[$name] = $value;
            } else {
                unset($params[$name]);
            }
        }

    }

    if (is_array($params)) {
        foreach ($params as $i => $t) {
            if (strlen($t) == 0) {
                unset($params[$i]);
            }
        }
    }

    $config = \Phpcmf\Service::M('app')->get_config('hy');
    $rule = !$params && !$name ? $config['search']['urlrule'] : $config['search']['urlrule_page'];
    if ($rule) {
        $data['param'] = dr_search_rewrite_encode($params, null);
        if ($params && !$data['param']) {
            log_message('error', '黄页无法通过[搜索参数字符串规则]获得参数');
        }
        $rep = new \php5replace($data);
        $url = preg_replace_callback("#{([a-z_0-9]+)}#Ui", array($rep, 'php55_replace_data'), $rule);
        $url = preg_replace_callback('#{([a-z_0-9]+)\((.*)\)}#Ui', array($rep, 'php55_replace_function'), $url);
        return str_replace('//', '/', '/' . $url);
    } else {
        return '/index.php?s=hy&' . trim( @http_build_query($params), '&');
    }
}